package com.example.wwk.graduationpractice.SmackUtils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.wwk.graduationpractice.AsyncTask.LoginAsyncTask;
import com.example.wwk.graduationpractice.Globalutils.MyApplication;
import com.example.wwk.graduationpractice.Globalutils.NetWorkUtils;

import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.XMPPConnection;

import java.util.Timer;

/**
 * Created by wwk on 2017/3/31.、
 * 无法登陆监听
 */

public class ReConnectionListener implements ConnectionListener {
    private Timer timetask;
    String username;
    String password;
    private int logintime = 2000;
    String TAG = "123";
    Handler handler;

    public ReConnectionListener(Handler handler) {
        this.handler=handler;
    }

    @Override
    public void connected(XMPPConnection xmppConnection) {

    }

    @Override
    public void authenticated(XMPPConnection xmppConnection, boolean b) {

    }

    @Override
    public void connectionClosed() {
        Log.d(TAG, "connectionClosed: ");
    }

    @Override
    public void connectionClosedOnError(Exception e) {
        Message message = new Message();
        if (!NetWorkUtils.isNetworkConnected(MyApplication.getContext())) {//  判断是否 无网络
            Log.d(TAG, "connectionClosedOnError: 无网络");
            message.what = LoginAsyncTask.NOT_NETWORK;

        } else if (e.getMessage().toString().contains("conflict")) { //用户登录冲突
            Log.d(TAG, "connectionClosedOnError: 用户登录冲突");
            message.what = LoginAsyncTask.CONFLICT;
        }
        handler.sendMessage(message);
    }

    @Override
    public void reconnectionSuccessful() {

    }

    @Override
    public void reconnectingIn(int i) {

    }

    @Override
    public void reconnectionFailed(Exception e) {

    }

//    class TimeTask extends TimerTask {
//        @Override
//        public void run() {
//            username = SharedPreferenceUtils.getSharedPreferences().getString("user", "");
//            password = SharedPreferenceUtils.getSharedPreferences().getString("password", "");
//            if (username != null && password != null) {
//                // 连接服务器
//                AbstractXMPPConnection connection = XmppConnectUtils.getConnection();   //获取连接
//                try {
//                    connection.login(username, password);
//                    Presence presence = new Presence(Presence.Type.available);
//                    presence.setStatus("在线");
//                    connection.sendStanza(presence);       //设置状态
//                    MyApplication.setConnection(connection);
//                } catch (XMPPException e) {
//                    e.printStackTrace();
//                } catch (SmackException e) {
//                    e.printStackTrace();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

}
